package com.chaos.gateway.service.auth;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.chaos.framework.model.constant.SecurityConstants;
import com.chaos.framework.model.serivce.RedisService;
import com.chaos.gateway.dto.RolePermissionDto;
import com.chaos.gateway.entity.RbacRoleMenuBtn;
import com.chaos.gateway.mapper.RbacRoleMenuBtnMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
*
* @author yjp
* @since 2024-06-13
*/
@Slf4j
@Component
public class RbacRoleMenuBtnService extends ServiceImpl<RbacRoleMenuBtnMapper, RbacRoleMenuBtn> {

	@Autowired
	private RedisService redisService;

	public void setRolePermList() {
		List<RolePermissionDto> rolePermissionList = baseMapper.rolePermissionList();
		if (CollectionUtil.isEmpty(rolePermissionList)) {
			return;
		}

		Map<String, List<String>> rolePermissionMap =
				rolePermissionList.stream().collect(Collectors.groupingBy(RolePermissionDto::getUrlPerm, Collectors.mapping(RolePermissionDto::getRoleName, Collectors.toList())));

		redisService.hMset(SecurityConstants.AUTH_ROLE_PERMISSION_URL_KEY, rolePermissionMap);
	}

	public Map<String, List<RolePermissionDto>> getRolePermList() {
		return redisService.hMget(SecurityConstants.AUTH_ROLE_PERMISSION_URL_KEY);
	}

}
